<?php
/**
* traitement ajax du theme resto
* @author Gmax
*/
class modules_ajax extends controler {
    
    public static function validReservation($query){
        $datas = array();
        return array('success'=>true, 'datas'=>$datas);
    }

    public static function getCalandar($query){
        $action = (isset($query['action']))?$query['action']:'';
        $current = (isset($query['current'])&&$query['current']!=='')?$query['current']:tools_convert::getMois(date('m')).' '.date("Y");
        
        list($mois,$an) = explode(' ', $current,2);
        switch($action){
            case 'prev':{
                $currentTimeStamp = mktime(0, 0, 0, tools_convert::getNumMois($mois)-1, date("d"), $an);
                break;
            }
            case 'next':{
                $currentTimeStamp = mktime(0, 0, 0, tools_convert::getNumMois($mois)+1, date("d"), $an);
                break;
            }
            default :{
                $currentTimeStamp = mktime(0, 0, 0, tools_convert::getNumMois($mois), date("d"), $an);
                break;
            }
        }
        
        $datas=array();
        
        $prevMonth = tools_convert::getMois(date('m',mktime(0, 0, 0, date("m",$currentTimeStamp)-1, date("d"), date("Y",$currentTimeStamp))));
        $currentMonth = tools_convert::getMois(date('m',$currentTimeStamp));
        $nextMonth = tools_convert::getMois(date('m',mktime(0, 0, 0, date("m",$currentTimeStamp)+1, date("d"), date("Y",$currentTimeStamp))));

        $ts_firstDay = tools_convert::unix_timestamp(date('Y',$currentTimeStamp).'-'.date('m',$currentTimeStamp).'-01 00:00:00');
        $firstDay = date('N',$ts_firstDay);
        $nbJour = date('t',$currentTimeStamp);
        	
        $currentNumDay = 0;
        $html = '';
        while($currentNumDay<=$nbJour){
            $html .= '<tr>';
                for($j=1;$j<8;$j++){
                    if($currentNumDay===0 && $j==$firstDay){
                        $currentNumDay++;
                    }
                    if($currentNumDay>0 && $currentNumDay<=$nbJour){
                        if($currentNumDay===(date("d")+0) && date('m',$currentTimeStamp)===date('m')){
                            $html .= '<td><span class="current" date="'.$currentNumDay.'-'.date('m',$currentTimeStamp).'-'.date('Y',$currentTimeStamp).'">'.$currentNumDay.'</span></td>';
                        }elseif( date('Y',$currentTimeStamp)<date('Y') || date('m',$currentTimeStamp)<date('m') || (date('m',$currentTimeStamp)===date('m') && $currentNumDay<(date("d")+0))){
                            $html .= '<td><span class="disable" date="'.$currentNumDay.'-'.date('m',$currentTimeStamp).'-'.date('Y',$currentTimeStamp).'">'.$currentNumDay.'</span></td>';
                        }else{
                            $html .= '<td><span date="'.$currentNumDay.'-'.date('m',$currentTimeStamp).'-'.date('Y',$currentTimeStamp).'">'.$currentNumDay.'</span></td>';
                        }
                        $currentNumDay++;
                    }else{
                        $html .= '<td></td>';
                    }
                }
            $html .='</tr>';
        }
        $datas['htmlCalandar'] = $html;
        $datas['prevMonth'] = $prevMonth;
        $datas['nextMonth'] = $nextMonth;
        $datas['currentMonth'] = $currentMonth.' '.date('Y',$currentTimeStamp);
        
        return array('success'=>true, 'datas'=>$datas);
        
    }
}
?>